/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.action.auth.captcha.create.flow;

import cn.hallele.infrastructure.data.client.CacheClient;
import cn.hallele.infrastructure.metadata.constant.HttpConst;
import cn.hallele.infrastructure.util.IdUtil;
import cn.hallele.ms.infrastructure.plugin.captcha.CaptchaClient;
import cn.hallele.ms.infrastructure.plugin.captcha.PortalCaptcha;
import cn.hallele.ms.portalcenter.application.bo.CaptchaBO;
import cn.hallele.ms.starter.web.framework.node.ServiceFlow;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/12/31 20:40
 */
@Slf4j
@LiteflowComponent
public class CreateCaptchaFlow extends ServiceFlow {
    @Autowired
    private CaptchaClient captchaClient;

    @Override
    public void process() throws Exception {
        PortalCaptcha portalCaptcha = captchaClient.getPortalCaptcha();
        String code = portalCaptcha.getCode();
        String imageBase64Data = portalCaptcha.getImageBase64Data();
        String id = IdUtil.simpleUUID();
        CacheClient.setValue(HttpConst.CAPTCHA_KEY_PREFIX + id, code, portalCaptcha.getTtl());
        CaptchaBO captchaBO = CaptchaBO.builder().key(id).captcha(imageBase64Data).build();
        this.setResult(captchaBO);
    }
}
